public class DynArrayStack {
    int top;
    int capacity;
    int[] array;

    public DynArrayStack(int capacity){
        this.capacity = capacity;
        top = -1;
        array = new int[capacity];
    }

    boolean isDynArrayStackFull(){
        return (top == capacity -1);
    }

    boolean isDynArrayStackEmpty(){
        return (top == -1);
    }

    void push(int data){
        if(isDynArrayStackFull()){
            doubleStacak();
        }else{
            array[++top] = data;
            System.out.println("the "+data+" is push");
        }
    }

    int pop(){
        if(isDynArrayStackEmpty()){
            System.out.println("栈为空");
            return 0;
        }else{
            return array[top--];
        }
    }

    void deleteDynArrayStack(){
        top = -1;
    }

    /**
     * 数组容量翻倍
     */
    void doubleStacak(){
        int newArray[] = new int[capacity*2];
        System.arraycopy(array,0,newArray,0,capacity);
        capacity = capacity*2;
        array = newArray;
    }
}

class DynArrayStackTest{
    public static void main(String[] args) {
        DynArrayStack arrayStack = new DynArrayStack(5);
        for (int i=0;i<6;i++){
            arrayStack.push(i+1);
        }
        arrayStack.push(6);
        for (int i=0;i<5;i++){
            System.out.println("the "+arrayStack.pop()+" is pop");
        }
        arrayStack.push(7);
        System.out.println(arrayStack.pop());
    }
}
